<?php
if (!session_id()) {
    session_start();
}
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
function get_client_ip() {
     $ipaddress = '';
     if (getenv('HTTP_CLIENT_IP'))
         $ipaddress = getenv('HTTP_CLIENT_IP');
     else if(getenv('HTTP_X_FORWARDED_FOR'))
         $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
     else if(getenv('HTTP_X_FORWARDED'))
         $ipaddress = getenv('HTTP_X_FORWARDED');
     else if(getenv('HTTP_FORWARDED_FOR'))
         $ipaddress = getenv('HTTP_FORWARDED_FOR');
     else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
     else if(getenv('REMOTE_ADDR'))
         $ipaddress = getenv('REMOTE_ADDR');
     else
         $ipaddress = 'UNKNOWN';

     return $ipaddress; 
}
  // require 'src/facebook.php';  
  global $wpdb;

  $_SESSION['user_id'] = get_client_ip();
  $_SESSION['username'] = get_client_ip();
  $xuser = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE  user_id = '{$_SESSION['user_id']}'");
  if(!isset($xuser->id)){
    $tb = 'ap_painting_user' ;
    $data = array('name'=> $_SESSION['username'], 'user_id' => $_SESSION['user_id'] );
    $wpdb->insert($tb,$data,array('%s', '%s'));
  }

  if(isset($_SESSION['user_id']))
  $xuser = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE  user_id = '{$_SESSION['user_id']}'");
  
  /* if(!(isset($_SESSION['user_id']) && $_SESSION['user_id']) || (isset($_SESSION['user_id']) && !isset($xuser->id) )){ 

  // Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
  'appId' => '370672979703477',
  'secret' => 'b77d7179c518edeaa81adefa3f02ed01',
  ));

  // Get User ID
  $user = $facebook->getUser();


  // We may or may not have this data based on whether the user is logged in.
  //
  // If we have a $user id here, it means we know the user is logged into
  // Facebook, but we don't know if the access token is valid. An access
  // token is invalid if the user logged out of Facebook.

  if ($user) {
  try {
  // Proceed knowing you have a logged in user who's authenticated.
  $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
  error_log($e);
  $user = null;
  }
  } else {
  $loginUrl = $facebook->getLoginUrl();
  echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
  exit;
  }

  $_SESSION['user_id'] = $user;
  $_SESSION['username'] = $user_profile['name'];
  $xuser = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE  user_id = {$_SESSION['user_id']}");
  if(!isset($xuser->id)){
    global $wpdb;
    $tb = 'ap_painting_user' ;
    $data = array('name'=> $_SESSION['username'], 'user_id' => $_SESSION['user_id'] );
    $wpdb->insert($tb,$data,array('%s', '%s'));
  }else{
    global $wpdb;
    $tb = 'ap_painting_user' ;
    $data = array('name'=> $_SESSION['username'], 'user_id' => $_SESSION['user_id'] );
    $wpdb->update($tb,$data,array( 'id' => $xuser->id ),array('%s', '%s'));
  }
  }
  
  $xuser = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE  user_id = {$_SESSION['user_id']}");

  if(!isset($xuser->id) && !is_home()){
    wp_redirect( home_url() ); exit;
  }

 /*
if (is_home() && $_SESSION['user_id'] && 0 ) {
    $location = get_site_url() . '/the-le';
    wp_redirect($location);
} */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="vi" xml:lang="vi">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <meta charset="<?php bloginfo('charset'); ?>" />

        <title> <?php wp_title('|', true, 'right'); ?></title>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/jquery.js"></script>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/jquery-ui.js"></script>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/slimScroll.js"></script>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/snow.js"></script>
        <link href="<?php echo bloginfo('template_url'); ?>/css/style.css" type="text/css" rel="stylesheet" />
        <link href="<?php echo bloginfo('template_url'); ?>/css/animate.css" type="text/css" rel="stylesheet" />

        <?php //wp_head();   ?>
        
        <script type="text/javascript">
            var base_url = '<?php echo bloginfo('template_url'); ?>/';

        </script>
    </head>

    <body>
        <div class="container">
            <div class="header">
                <a href="<?php echo site_url() ?>"><img class="logo" src="<?php echo bloginfo('template_url'); ?>/img/logo.png"></a>
            </div>